import 'package:flutter/material.dart';

///可变状态widget
abstract class BaseMaterialStateWidget extends StatefulWidget {
  _BasePageState __basePageState;

  @protected
  String getTitle();

  @override
  State<StatefulWidget> createState() {
    return __basePageState = _BasePageState(this);
  }

  @protected
  Widget buildContent(BuildContext context);

  ///发生状态改变时使用
  void setState(VoidCallback fn) {
    __basePageState.setState(fn);
  }

  @protected
  void initState(BuildContext context) {}

  @protected
  void dispose() {}
}

class _BasePageState extends State<BaseMaterialStateWidget> {
  BaseMaterialStateWidget baseMaterialStateWidget;

  _BasePageState(this.baseMaterialStateWidget);

  @override
  void initState() {
    super.initState();
    baseMaterialStateWidget.initState(context);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: this.baseMaterialStateWidget.getTitle() == null
          ? ""
          : baseMaterialStateWidget.getTitle(),
      home: Scaffold(
        appBar: baseMaterialStateWidget.getTitle() == null
            ? null
            : AppBar(title: Text(this.baseMaterialStateWidget.getTitle())),
        body: this.baseMaterialStateWidget.buildContent(context),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    baseMaterialStateWidget.dispose();
  }
}
